<?php
class PigcmsinterfaceAction extends Action {
	public $siteInfo = array();
    public function initCommon()
    {
        header("content-type:text/html; charset=utf-8;");
        defined('__RELATEID__') or die('您可能还没有绑定域名');
        defined('__SITEID__') or die('找不到这个站点');
        $site = M('Site')->where(array('id' => __SITEID__))->find();
        $this->siteInfo = $site;
        if (!$site['status'] > 0) {
            $this->error('该站点被禁用');
        }
        if (intval($site['timeout']) < intval(time())) {
            $this->error('该站点已经过期');
        }
		$this->memberInfo = session('memberInfo');
        //dump(checkmobile());exit;
        if ($this->siteInfo['mobileUrl'] != '' && checkmobile()) {
            header("location:" . $this->siteInfo['mobileUrl']);
            exit;
        }if(isset($_GET['dump'])){dump(__RELATEID__);}
        $siteurl = C('DOMAIN_MAIN');
        $this->siteurl = $siteurl[0];
        //多站点AFF_ID错误
        if (isset($_COOKIE['aff_id'])) {
			
        }
        if (isset($_GET['aff_id']) && intval($_GET['aff_id']) > 0) {
            $id = intval($_GET['aff_id']);
            $this->aff_id = $id;
        }
        $member = M('Member')->where(array('site_id' => __RELATEID__, 'id' => intval($this->aff_id)))->find();
        if (!$member) {
            $this->aff_id = 0;
        } else {
            setcookie('aff_id', $this->aff_id, time() + 300, '/');
        }

    } 

	public function _initialize(){
		//加载微信配置项
		$this->initCommon();
		Vendor('Weixin.Weixin');
		$d['token']=getSysVal('token');
        $d['appid']=getSysVal('appid');
        $d['appsecret']=getSysVal('appsecret');
        $this->weixin=new WeixinChat($d);
	}
	
	//获取接口access_token
	public function getaccess_token(){ 
		echo $this->weixin->getAccessToken();
		
	} 
	//获取接口access_token
	public function getJsdkTicket(){ 
		echo $this->weixin->getJsdkTicket();
		
	}
	//获取用户信息
	public function getmemberinfo(){
		if($this->siteInfo['is_wxLogin'] == 1){ 
			$snsapi_base = 1;
		}elseif($this->siteInfo['is_wxLogin'] == 2){
			$snsapi_base = 0;
		}
		if(isset($_GET['state'])){
			$state = $_GET['state'];
		}
		$callback = $_GET['jumpUrl'];
		$callbackflag = md5($callback);
		S($callbackflag, base64_decode($callback));
	    $this->weixin->redirectGetOauthCode('http://' . $_SERVER['HTTP_HOST'] . __APP__ . "/Weixin/Pigcmsinterface/getcode.html", $snsapi_base, $callbackflag);
    }
	
	
	
	public function getsnstoken(){
		$access_token = $this->weixin->getSnsAccessToken($_GET['code']);
		if($this->siteInfo['is_wxLogin'] == 1){
            $login_result = $this->weixin->getSnsUserInfo($access_token['access_token'], $access_token['openid'],2);
		}else{
		    $login_result = $this->weixin->getUserInfo($access_token['openid'],2);  
		}
		echo $login_result;
	}
	
	public function getcode() {
        $access_token = $this->weixin->getSnsAccessToken($_GET['code']);
		//dump($access_token);
		if($this->siteInfo['is_wxLogin'] == 1){
            $login_result = $this->weixin->getSnsUserInfo($access_token['access_token'], $access_token['openid'],2);
		}else{
		    $login_result = $this->weixin->getUserInfo($access_token['openid'],2);  
		}
		//exit(S($_GET['state']).'&memberinfo='.$login_result);
		S(md5($access_token['openid']),$login_result);
		//dump($login_result);exit;
		header("Location:".S($_GET['state']).'&state=oauth&code'.$_GET['code'].'&memberinfo='.md5($access_token['openid']));
    } 
	public function getuserinfo(){
		$flag = $_GET['flag']; 
		echo S($flag);
	}
	
}
 